package com.zznode.redsun.smartcrm.business.action;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.csp.domain.commons.query.Page;
import com.zznode.redsun.commons.dto.CustomerDto;
import com.zznode.redsun.commons.dto.CustomerQuery;
import com.zznode.redsun.commons.utils.AbstractBaseAction;
import com.zznode.redsun.smartcrm.business.service.CustomerService;
import com.zznode.redsun.smartcrm.model.CardCategory;
import com.zznode.redsun.smartcrm.model.Customer;
import com.zznode.redsun.smartcrm.model.CustomerLevel;
import com.zznode.redsun.smartcrm.model.CustomerType;
import com.zznode.redsun.smartcrm.model.MonthlyIncome;
import com.zznode.redsun.smartcrm.model.PersonalHobby;
import com.zznode.redsun.smartcrm.model.Profession;

@Controller
@Namespace("/business/customer")
public class CustomerAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private CustomerDto customer;
	private Page<JSONObject> customerPage;
	private CustomerQuery params;

	@Autowired
	private EntityManager entityManager;
	@Autowired
	private CustomerService customerService;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		loadCategories();
		return SUCCESS;
	}

	@Action(value = "query", results = { @Result(name = SUCCESS, location = "query.jsp") })
	public String query() {
		// loadCategories();
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.customer = CustomerDto.create();
		loadCategories();
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		this.customer = this.customerService.get(Long.valueOf(id));
		loadCategories();
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null == this.customer) {
			this.setJsonMessage("保存失败，提交数据为空.");
		} else {
			this.customerService.save(customer, this.getCurrentUser());
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String remove() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.entityManager.deleteById(Customer.class, id);
		} else {
			this.setJsonMessage("删除失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = { "root", "customerPage",
			"excludeNullProperties", "true" }) })
	public String list() {
		this.customerPage = customerService.queryForPage(getParams(), getPageNumber(), getPageSize());
		// customerService.fillDict(this.customerPage.getRows());
		return SUCCESS;
	}

	private void loadCategories() {
		this.putValueStack("types", entityManager.getAll(CustomerType.class));
		this.putValueStack("levels", entityManager.getAll(CustomerLevel.class));
		this.putValueStack("incomes", entityManager.getAll(MonthlyIncome.class));
		this.putValueStack("professions", entityManager.getAll(Profession.class));
		this.putValueStack("cardCategories", entityManager.getAll(CardCategory.class));
		this.putValueStack("hobbies", entityManager.getAll(PersonalHobby.class));
	}

	public CustomerDto getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerDto customer) {
		this.customer = customer;
	}

	public CustomerQuery getParams() {
		return params;
	}

	public Page<JSONObject> getCustomerPage() {
		return customerPage;
	}

	public void setCustomerPage(Page<JSONObject> customerPage) {
		this.customerPage = customerPage;
	}

	public void setParams(CustomerQuery params) {
		this.params = params;
	}
}
